package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.CommonDao;
import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectStatusDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProject;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectStatus;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;
import util.bysjException;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class GraduateProjectStatusService {
	private static GraduateProjectStatusDao graduateProjectStatusDao= GraduateProjectStatusDao.getInstance();
	private static GraduateProjectStatusService graduateProjectStatusService=new GraduateProjectStatusService();


	public static GraduateProjectStatusService getInstance(){
		return graduateProjectStatusService;
	}

	public Collection< GraduateProjectStatus> findAll
			(List<Condition> conditionList_json_str, Pagination pagination)
			throws SQLException {

		Connection connection= JdbcHelper.getConn();
		Collection< GraduateProjectStatus> graduateProjectStatuses =
				graduateProjectStatusDao.findAll(conditionList_json_str,pagination,connection);
		JdbcHelper.close(connection);
		return  graduateProjectStatuses;
	}

	public GraduateProjectStatus find(Integer id) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		GraduateProjectStatus result= graduateProjectStatusDao.find(id,connection);
		JdbcHelper.close(connection);
		return  result;
	}

	public boolean update( GraduateProjectStatus  graduateProjectStatus) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		boolean result= graduateProjectStatusDao.update(graduateProjectStatus,connection);
		JdbcHelper.close(connection);
		return result;
	}

	public boolean add( GraduateProjectStatus  graduateProjectStatus) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		boolean result= graduateProjectStatusDao.add(graduateProjectStatus,connection);
		JdbcHelper.close(connection);
		return  result;
	}

	public boolean delete(Integer id) throws SQLException, bysjException {
		Connection connection = JdbcHelper.getConn();
		GraduateProjectStatus  graduateProjectStatus = this.find(id);
		boolean result= this.delete(graduateProjectStatus,connection);
		JdbcHelper.close(connection);
		return result;
	}

	public boolean delete(GraduateProjectStatus graduateProjectStatus,
						  Connection connection) throws SQLException, bysjException {
		boolean result=false;
		int countTeacherOfProfTitle = CommonDao.getInstance().
				countWhereForeignKey(connection,"graduateProject","graduateProjectStatus_id",graduateProjectStatus.getId());
		//若没有二级单位，则能够删除
		if(countTeacherOfProfTitle==0){
			result = graduateProjectStatusDao.delete(graduateProjectStatus,connection);
		}else {
			throw new bysjException( graduateProjectStatus.getDescription()+"有下属系，不能删除");
		}
		JdbcHelper.close(connection);
		return result;
	}
}
